<?php
// 构造查询语句
$stateCounts = DB::table('parts')
    ->select('state', DB::raw('COUNT(*) as count'))
    ->groupBy('state')
    ->get();

// 将查询结果转换为数组
$data = [];
foreach ($stateCounts as $stateCount) {
    $data[] = [
        'value' => $stateCount->count,
        'name' => $stateCount->state,
    ];
}

// 编码数组为 JSON 字符串
$chartData = json_encode($data);
?>

<div id="partsstate" style="height:200px;box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);margin-bottom:30px;"></div>
<script type="text/javascript">
var chartDom = document.getElementById('partsstate');
var myChart = echarts.init(chartDom);
var option;

option = {
  title: {
    text: '配件状态',
    left: 'center'
  },
  tooltip: {
    trigger: 'item'
  },
  legend: {
    orient: 'vertical',
    left: 'left'
  },
  series: [
    {
      name: 'State',
      type: 'pie',
      radius: '50%',
      data: <?php echo $chartData; ?>,
      label: {
        show: true,
        formatter: '{b}：{c}'
      },
      emphasis: {
        itemStyle: {
          shadowBlur: 10,
          shadowOffsetX: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
        
      }
    }
  ]
};

option && myChart.setOption(option);
</script>